package com.lingyuan.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingyuan.common.core.domain.vo.ums.MemberAddressVo;
import com.lingyuan.common.core.exception.ServiceException;
import com.lingyuan.common.core.utils.bean.BeanUtils;
import com.lingyuan.ums.domain.MemberAddressEntity;
import com.lingyuan.ums.mapper.MemberAddressMapper;
import com.lingyuan.ums.service.MemberAddressService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName：MemberAddressServiceImpl
 * @Author: LeiHao
 * @Date: 2024/9/21 12:08
 * @Description: 会员收货地址业务层
 */
@Service
public class MemberAddressServiceImpl extends ServiceImpl<MemberAddressMapper, MemberAddressEntity> implements MemberAddressService {
    /* *
     * @Title: findMemberByIdAddressList
     * @Author: DELL
     * @Date: 2024-09-21 12:06:39
     * @Params: [userId]
     * @Return: List<MemberAddressVo>
     * @Description: 根据用户id查询会员收货地址
     */
    @Override
    public List<MemberAddressVo> findMemberByIdAddressList(Long userId) {
        List<MemberAddressEntity> memberAddressEntityList = this.list(new QueryWrapper<MemberAddressEntity>().lambda().eq(MemberAddressEntity::getMemberId, userId));
        if (CollectionUtils.isEmpty(memberAddressEntityList)){
            throw new ServiceException("您还没有具体的收货地址,请添加");
        }
        List<MemberAddressVo> memberAddressVoList = memberAddressEntityList.stream().map(memberAddressEntity -> {
            MemberAddressVo memberAddressVo = new MemberAddressVo();
            //Bean拷贝
            BeanUtils.copyBeanProp(memberAddressVo, memberAddressEntity);
            return memberAddressVo;
        }).collect(Collectors.toList());
        return memberAddressVoList;
    }


    /* *
     * @Title: findById
     * @Author: DELL
     * @Date: 2024-09-21 12:06:56
     * @Params: [id]
     * @Return: MemberAddressVo
     * @Description: 根据id查询会员收货地址
     */
    @Override
    public MemberAddressVo findById(Long id) {
        MemberAddressEntity memberAddressEntity = this.getOne(new QueryWrapper<MemberAddressEntity>().lambda().eq(MemberAddressEntity::getId, id));
        if (null == memberAddressEntity){
            throw new ServiceException("查询地址编号:"+id+"无效");
        }
        MemberAddressVo memberAddressVo = new MemberAddressVo();
        //Bean拷贝
        BeanUtils.copyProperties(memberAddressEntity,memberAddressVo);
        return memberAddressVo;
    }
}
